package days32.title67;

import org.junit.Test;

public class Solution {
    // @Test
    // public void test(){
    //     String a = "1";
    //     String b = "111";
    //     System.out.println("addBinary(a,b) = " + addBinary(a, b));
    // }
    public String addBinary(String a, String b) {
        int add = 0;
        String result = "";
        int aIndex = a.length()-1;
        int bIndex = b.length()-1;
        for (; aIndex >= 0 && bIndex >= 0; aIndex--,bIndex--) {
            int num = Character.getNumericValue(a.charAt(aIndex))+Character.getNumericValue(b.charAt(bIndex))+add;
            if (num >= 2){
                num = num%2;
                add = 1;
            }
            else
                add = 0;
            result = num + result;
        }
        if (aIndex!=-1){
            for (; aIndex >= 0 ; aIndex--){
                int num = Character.getNumericValue(a.charAt(aIndex))+add;
                if (num >= 2){
                    num = num%2;
                    add = 1;
                }
                else
                    add = 0;
                result = num + result;
            }

        }
        if (bIndex!=-1){
            for (; bIndex >= 0 ; bIndex--){
                int num = Character.getNumericValue(b.charAt(bIndex))+add;
                if (num >= 2){
                    num = num%2;
                    add = 1;
                }
                else
                    add = 0;
                result = num + result;
            }
        }
        if (add!=0)
            result = add + result;
        return result;
    }
}